package com.hs.controller;

import com.github.pagehelper.PageInfo;
import com.hs.entity.Course;
import com.hs.service.CourseService;
import com.hs.util.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("course")
public class CourseController {
    @Autowired
    private CourseService courseService;

    @GetMapping("{id}")
    public R getCourseById(@PathVariable("id") int id) {
        Course cc = courseService.getCourseByid(id);
        return R.ok(cc);
    }

    @DeleteMapping("{id}")
    public R deleteCourseById(@PathVariable("id") int id) {
        int i = courseService.deleteCourseByid(id);
        return R.ok(i);
    }

    @PostMapping("add")
    public R addCourse(@RequestBody Course course) {
        int i = courseService.addCourse(course);
        return R.ok(i);
    }

    @PutMapping("update")
    public R updateCourse(@RequestBody Course course) {
        int i = courseService.updateCourse(course);
        return R.ok(i);
    }

    @GetMapping("page")
    public R getCourseByPage(Course course, @RequestParam(required = false, defaultValue = "1") int pageNum, @RequestParam(required = false, defaultValue = "2") int pageSize) {
        return R.ok(courseService.getCourseByPage(course, pageNum, pageSize));
    }

    @GetMapping
    public R getcourselist(Course course) {
        PageInfo<Course> getcourselist = courseService.getcourselist(course);
        return R.ok(getcourselist);
    }

    @GetMapping("pageByConsultantId")
    public R getAllCourseByConsultantId(int consultantId,
                                        @RequestParam(required = false, defaultValue = "1") int pageNum,
                                        @RequestParam(required = false, defaultValue = "5") int pageSize) {
        PageInfo<Course> courseByConsultantId = courseService.getCourseByConsultantId(consultantId, pageNum, pageSize);
        return R.ok(courseByConsultantId);
    }

    @GetMapping("sort")
    public R getCourseBySort(@RequestParam(required = false, defaultValue = "1") Integer pageNum, @RequestParam(required = false, defaultValue = "2") Integer pageSize) {
        PageInfo<Course> cc = courseService.getcourselistBysort(pageNum, pageSize);
        return R.ok(cc);
    }

    @GetMapping("sorta")
    public R getCourseBySorta(@RequestParam(required = false, defaultValue = "1") Integer pageNum, @RequestParam(required = false, defaultValue = "2") Integer pageSize) {
        PageInfo<Course> aa = courseService.getcourselistBysorta(pageNum, pageSize);
        return R.ok(aa);
    }
    @GetMapping("bypricerange")
    public R getpricerange(@RequestParam(required = false, defaultValue = "1") Integer pageNum,
                           @RequestParam(required = false, defaultValue = "2") Integer pageSize,int minPrice,int maxPrice){
        PageInfo<Course> ss = courseService.selectbypricerange(pageNum, pageSize, minPrice, maxPrice);
        return R.ok(ss);
    }
}